package com.cp.order.platform.web;

import java.util.HashMap;
import java.util.Map;

import com.cp.order.model.GoodsType;
import com.cp.order.platform.BaseController;

public class GoodsTypeController extends BaseController {

	public void index() {
		renderFreeMarker("type.html");
	}

	public void addType() {
		renderFreeMarker("typeAdd.html");
	}

	public void editType() {
		renderFreeMarker("typeEdit.html");
	}

	public void list() {
		int cpage = getParaToInt("cpage", 1);
		int pageSize = getParaToInt("pagesize", 10);
		HashMap<String, String> params = new HashMap<>();
		returnOK(GoodsType.dao.queryList(params, cpage, pageSize));
	}

	public void add() {
		GoodsType goodsType = getModel(GoodsType.class, "type");
		goodsType.setStatus(0);
		if (goodsType.save()) {
			returnOK();
		} else {
			returnBusinessError("添加失败");
		}
	}

	public void update() {
		GoodsType goodsType = getModel(GoodsType.class, "type");
		if (goodsType.update()) {
			returnOK();
		} else {
			returnBusinessError("修改失败");
		}
	}

	public void delete() {
		int id = getParaToInt("id");
		GoodsType goodsType = new GoodsType();
		goodsType.setId(id);
		goodsType.setStatus(1);// 逻辑删除
		if (goodsType.update()) {
			returnOK();
		} else {
			returnBusinessError("删除失败");
		}
	}

	public void judge() {
		GoodsType goodsType = GoodsType.dao.judgeName(getPara("typeName"));
		Map<String, Object> map = new HashMap<String, Object>();
		if (goodsType == null) {
			map.put("isHas", false);
		} else {
			map.put("isHas", true);
		}
		returnOK(map);
	}

}
